import axios from 'axios';
import store from '../store';
import MyNotify from '../utils/notify';

const baseURL = import.meta.env.VITE_API_HOST;

const tokenPrefix = 'Bearer ';

const instance = axios.create({
  baseURL
});

instance.interceptors.request.use(
  config => {
    // do something before request is sent

    if (store.state.user.token) {
      config.headers['Authorization'] = tokenPrefix + store.state.user.token;
    }
    return config;
  },
  error => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  response => {
    if (response.data.code) {
      handleErrorResponse(response);
      return Promise.reject(response.data);
    }
    return response.data;
  },

  error => {
    handleErrorResponse(error.response);
    return Promise.reject(error);
  }
);

const handleErrorResponse = response => {
  if (response.status === 401 || response.status === 403) {
    store.dispatch('user/logout').then(() => window.location.reload());
  }

  if (response.data instanceof Array) {
    response.data.forEach(item => {
      MyNotify.failure(item.message);
    });
  } else if (response.state === 200) {
    MyNotify.success(response.data.message);
  } else {
    MyNotify.failure(response.data.message);
  }
};

const { get, post, put } = instance;

export { get, post, put };
